<template>
    <div class="app-container">
      <div class="maindiv">
        <!-- {{ e }} -->
        <div class="ces-main">
          <!-- style=" width: 350px;position: absolute;top: -2px;right: -20px;" -->
          <!-- style="position: absolute;top: 5px;right: 2px;top:8px" -->
          <Search-Form ref="form" size="mini" labelWidth="50px" :searchData="SearchGroup" :tableCols="tableCols"
            :searchHandle="searchHandle" @handleEnter="refresh"></Search-Form>
          <v-GetToolBar style="position: absolute;right: 2px;top:11px" align="right" :key="MenuCode" :MenuCode="MenuCode"
            :IsfunEdit="IsfunEdit" :Isfunfail="Isfunfail" @handeAdd="handeAdd" @handleEdit="handleEdit"
            @OpenFindForm="searchTop" @handlexportData="handlexportData" @IsfunEdit="handleIsfunEdit"
            @IsfunDelete="handleIsfunfail" @handleclose="handleclose"></v-GetToolBar>
  
          <div style="float: right;">
            <el-select class="elselest" v-model="value" placeholder="请选择">
              <el-option v-for="item in options" :key="item.ProcessGroupID" :label="item.ProcessGroupName"
                :value="item.ProcessGroupID">
              </el-option>
            </el-select>
            <el-button type="primary" size="mini" @click="configure">配置工艺</el-button>
          </div>
          <ces-table class="tableC" :ToolBarheight="ToolBarheight" size="mini" :isSelection="true" :isIndex="true"
            :isSearch="true" :isHandle="false" :IsfunEdit="IsfunEdit" :Isfunfail="Isfunfail" :tableData="tableData"
            :tableCols="tableCols" :Search="SearchGroup" @refresh="refresh" @handleEdit="handleEdit"
            @handleDelete="delData" @handrowclick="handrowclick" @select="selectMata" @selectAll="selectMata">
          </ces-table>
        </div>
      </div>
  
      <!--搜索弹框-->
      <el-dialog title="搜索" :visible.sync="SearchDialog" custom-class="dialogClass" class="searchDialog" center
        ref="searchDialog" :modal="false" :close-on-click-modal="true">
        <el-form :model="SearchGroup" label-width="90px" ref="searchForm">
          <Search-Form ref="form" :frame="true" size="mini" labelWidth="50px" :searchData="Search" :tableCols="tableCols"
            :searchHandle="searchHandle" @handleEnter="refresh"></Search-Form>
        </el-form>
        <div slot="footer" :style="{ 'margin-buttom': 0 == 0 ? DialogBtnMargin : DialogBtnMargin }">
          <el-button @click="reform()">重置</el-button>
          <el-button type="primary" @click="refresh()">确 定</el-button>
        </div>
      </el-dialog>
  
      <el-dialog v-if="dialogVisible" :visible.sync="dialogVisible" width="800px" top="20px">
        <el-form :model="name" label-width="80px" label-position="right">
          <el-tabs type="border-card">
            <el-tab-pane label="">
              <el-form :model="FormData" label-width="90px" ref="MianForm">
                <Mian-Form ref="form" labelWidth="100px" :FormData="FormData" :tableCols="tableCols" :StepSN="0"
                  :searchHandle="searchHandle"></Mian-Form>
              </el-form>
            </el-tab-pane>
            <div style="float:right ">
              <el-button type="primary" @click="handleOpenClose()">关闭</el-button>
              <el-button type="primary" @click="handleSave()">保存</el-button>
            </div>
          </el-tabs>
        </el-form>
      </el-dialog>
    </div>
  </template>
  
  <script>
  //引入组件
  
  import cesTable from "../Common//ComVxeTable.vue";
  // import cesTable from "../Common//table.vue";
  import SearchForm from "../Common//Search.vue";
  import MianForm from "../Common//form.vue";
  
  import { funDownload, funDownloadv1, GetTableColsData } from "@/api/Common";
  import {
    getDataList,
    DeleteForm,
    CloseForm,
    UpdateForm,
    getfunDownload,
    MenuUpdate,
  } from "@/api/CommonPage/CommonPage"; //更改
  import vGetToolBar from "../Common/GetToolBar";
  
  export default {
    name: "ProcessGroup",
    data() {
      //表单查询一般分为input，下拉框，和日期，下面是下拉框时的设置下拉信息的情况，我的项目没有用到，但是留着方便看
  
      return {
        key: 1,
        see: {},
        input: "",
        ToolBarheight: null,
        Id: null,
        index: null,
        StagingDataInfo: null,
        technologyID: null,
        dialogVisibleInfo: false,
        MenuCode: "A051111",
        tableData: [], //表格数据
        tableCols: [], //表格列数据
        FormData: [], //表格数据
        tableColsInfo: [],
        tableDataInfo: [],
        searchHandle: [],
        billID: "",
        IsfunEdit: true,
        Isfunfail: false,
        SearchGroup: {
          // 查询条件
          total: 0, // 总记录数
          pageSize: 100, // 一页显示的记录数
          page: 1, // /当前页码
          RNO: "1630", // 表编号  根据Controller中对应的表编号
          InterID: "1630",
          jsWhere: "", // 自定义条件
          Download: "",
          YearMonth: "",
          OrganizeID: "",
          LineName: "",
          IsFieldText: "0",
          // Status: 0,
          //   BillState: "BillStateALL",
        },
        SearchGroupInfo: {
          // 查询条件
          total: 0, // 总记录数
          pageSize: 100, // 一页显示的记录数
          page: 1, // /当前页码
          RNO: "1594", // 表编号  根据Controller中对应的表编号 //更改
          InterID: "1594",
          jsWhere: "", // 自定义条件
          Download: "",
          WorkingDate: [],
          OrganizeID: "",
          IsFieldText: "0",
        },
        SearchTechnology: {
          // 查询条件
          total: 0, // 总记录数
          pageSize: 100, // 一页显示的记录数
          page: 1, // /当前页码
          RNO: "1631", // 表编号  根据Controller中对应的表编号 //更改
          InterID: "1631",
          jsWhere: "", // 自定义条件
          Download: "",
          IsFieldText: "0",
        },
  
        cities: [
          {
            value: "ALL",
            label: "ALL",
          },
          {
            value: "0",
            label: "草稿",
          },
          {
            value: "1",
            label: "审核中",
          },
          {
            value: "2",
            label: "结案",
          },
          {
            value: "3",
            label: "作废",
          },
        ],
        BillStatecities: [
          {
            value: "BillStateALL",
            label: "ALL",
          },
          {
            value: "0",
            label: "未完成",
          },
          {
            value: "2",
            label: "已完成",
          },
        ],
        SearchDialog: false,
        DialogBtnMargin: 0,
        dialogVisible: false,
        options: [],
        value: "",
        selectData: [],
      };
    },
  
    components: {
      //引入组件后注册组件
      cesTable,
      vGetToolBar,
      SearchForm,
      MianForm,
    },
    mounted() {
      this.ToolBarheight = parseInt(document.documentElement.clientHeight * 0.3);
      this.getDataList();
    },
  
    methods: {
      async searchTop() {
        const res = await getDataList({ data: JSON.stringify(this.SearchGroup) });
        this.SearchGroup.total = res.data.Table1[0].RecordCount;
        this.tableData = res.data.Table;
      },
      selectMata(data) {
        this.selectData = data;
      },
      //配置工艺
      configure() {
        let flag = false;
        let result1
        this.selectData.forEach(async (obj) => {
          let ProcessName = "";
          this.options.forEach((o) => {
            if (o.ProcessID === this.value) ProcessName = o.ProcessName;
          });
  
          let data = {
            ProcessGroupMaterialID: obj.ProcessGroupMaterialID,
            MaterialID: obj.MaterialID,
            ProcessGroupID: this.value,
            Status: 1,
          };
  
          const result = await UpdateForm({
            IsPostBack: false,
            InterID: this.SearchTechnology.InterID,
            SavaDate: JSON.stringify(data),
          });
  
          result.data1.Table[0].MaterialID = obj.MaterialID;
          result.data1.Table[0].ProcessGroupID = this.value;
          result.data1.Table[0].Status = 1;
          result1 = await UpdateForm({
            IsPostBack: true,
            InterID: this.SearchTechnology.InterID,
            SavaDate: JSON.stringify(result.data1.Table[0]),
          });
          if (result1.code === 20000) {
            flag = true;
          }
        });
        setTimeout(async () => {
          if (flag) {
            this.$notify({
              title: result1.msg,
              dangerouslyUseHTMLString: true,
              message: "",
              type: "success",
            });
            const res = await getDataList({ data: JSON.stringify(this.SearchGroup) });
            this.SearchGroup.total = res.data.Table1[0].RecordCount;
            this.tableData = res.data.Table;
            // this.getDataList();
            // this.selectMata();
          } else {
            this.$notify({
              title: result1.msg,
              dangerouslyUseHTMLString: true,
              message: "",
              type: "error",
            });
          }
        }, 1500);
      },
  
      refresh() {
        this.getDataList();
      },
  
      reform() {
        this.SearchGroup = {
          // 查询条件
          // total: 0, // 总记录数
          pageSize: 13, // 一页显示的记录数
          page: 1, // /当前页码
          RNO: "1626", // 表编号  根据Controller中对应的表编号 //更改
          InterID: "1626",
          jsWhere: "", // 自定义条件
          Download: "",
          //   Status: 0,
          //   BillState: "BillStateALL",
        };
        // this.Search.InterID = this.InterID;
        // this.Search.RNO = this.InterID;
        this.getDataList();
      },
  
      async getDataList() {
        //表头数据
  
        const res = await getDataList({ data: JSON.stringify(this.SearchGroup) });
        this.SearchGroup.IsFieldText = "1"; //已加载数据，不再加载
        this.SearchGroup.total = res.data.Table1[0].RecordCount;
        this.tableData = res.data.Table;
        this.tableCols = res.data1;
  
        const resCols = await getDataList({
          data: JSON.stringify(this.SearchGroupInfo),
        });
        this.SearchGroupInfo.IsFieldText = "1"; //已加载数据，不再加载
        this.options = resCols.data.Table;
      },
  
      OpenFindForm() {
        // alert(JSON.stringify(this.tableCols))
        //打开搜索框
        this.SearchDialog = true;
        //计算外边距
        console.log(this.$refs.searchDialog.offsetHeight);
        this.DialogBtnMargin = this.$refs.searchDialog.offsetHeight - 40;
      },
  
      async handlexportData() {
        this.SearchGroup.Download = "1";
        const res = await getfunDownload({
          data: JSON.stringify(this.SearchGroup),
        });
        this.SearchGroup.Download = "0";
        funDownloadv1(res); //更改
      },
  
      handleIsfunEdit() {
        this.IsfunEdit = true;
      },
      handleIsfunfail() {
        this.Isfunfail = true;
      },
      async handeAdd() {
        // 新建
        const result = await UpdateForm({
          IsPostBack: false,
          InterID: this.SearchGroup.InterID,
        });
        this.FormData = result.data1.Table[0];
        this.dialogType = "edit";
        this.dialogVisible = true;
        this.checkStrictly = true;
  
        this.$nextTick(() => {
          const routes = this.generateRoutes(this.EditObj.routes);
          this.$refs.tree.setCheckedNodes(this.generateArr(routes));
          // set checked state of a node not affects its father and child nodes
          this.checkStrictly = false;
        });
        this.getDataList();
      },
      async handleEdit(scope) {
        // alert(scope.row)
        // 修改
        this.dialogVisible = true;
        const result = await UpdateForm({
          IsPostBack: false,
          InterID: this.SearchGroup.InterID,
          SavaDate: JSON.stringify(scope.row),
        });
        this.FormData = result.data1.Table[0];
        // alert(JSON.stringify(this.FormData))
        this.dialogType = "edit";
        this.checkStrictly = true;
        this.$nextTick(() => {
          const routes = this.generateRoutes(this.EditObj.routes);
          this.$refs.tree.setCheckedNodes(this.generateArr(routes));
          this.checkStrictly = false;
        });
        this.getDataList();
      },
  
      async handleSave() {
        // 修改
        this.FormData;
        debugger;
        const result = await UpdateForm({
          IsPostBack: true,
          InterID: this.SearchGroup.InterID,
          SavaDate: JSON.stringify(this.FormData),
        });
        this.FormData = result.data1.Table[0];
        if (result.code === 20000) {
          this.$notify({
            title: result.msg,
            dangerouslyUseHTMLString: true,
            message: "",
            type: "success",
          });
          this.getDataList();
        } else {
          this.$notify({
            title: result.msg,
            dangerouslyUseHTMLString: true,
            message: "",
            type: "error",
          });
        }
        this.dialogVisible = false;
      },
  
      async delData({ $index, row }) {
        // this.$confirm('删除操作, 是否继续?', '提示', {
        //     confirmButtonText: "确认",
        //     cancelButtonText: "取消",
        // })
        //     .then(async () => {
  
        // this.see = row
        const result = await DeleteForm({
          InterID: this.SearchGroup.InterID,
          SavaDate: JSON.stringify(row),
          IsPostBack: false,
        });
        this.getDataList();
        //   this.tableData.splice($index, 1);
        this.$message({
          type: "success",
          message: result.msg,
        });
  
        // })
        // .catch((err) => {
        //     console.error(err);
        // });
      },
  
      async handrowclick(row) { },
  
      handleOpenClose() {
        this.dialogVisible = false;
        this.dialogVisibleInfo = false;
      },
      handleclose() {
        if (this.billID != "") {
          this.$confirm("关单操作, 是否继续? 订单号：" + this.billID, "提示", {
            confirmButtonText: "确认",
            cancelButtonText: "取消",
          })
            .then(async () => {
              const result = await CloseForm({
                billID: this.billID,
                types: 1,
                IsPostBack: false,
              });
              this.getDataList();
              this.$message({
                type: "success",
                message: result.msg,
              });
            })
            .catch((err) => {
              console.error(err);
            });
        } else {
          this.$notify({
            title: "关单操作，请选择数据" + this.billID,
            dangerouslyUseHTMLString: true,
            message: "",
            type: "error",
          });
        }
      },
    },
  };
  </script>
  
  <style lang="scss" scoped>
  .app-container {
    width: 100%;
    overflow: hidden;
  
    .EditObjs-table {
      margin-top: 30px;
    }
  
    .permission-tree {
      margin-bottom: 5px;
    }
  
    .maindiv {
      display: flex;
      flex-wrap: wrap;
      width: 100%;
  
      .ces-main {
        position: relative;
        flex: 1;
        width: 80vw;
        overflow: outo;
  
        .elselest {
          width: 200px;
          transform: scale(0.9) translateY(2px);
        }
  
        .tableC {
          margin-top: 40px;
        }
      }
  
      .midMain {
        padding-top: 12px;
        width: 80vw;
        flex: 1;
        position: relative;
  
        .GetToolBar {
          position: absolute;
          right: 2px;
          top: 23px;
        }
      }
    }
  }
  
  .searchDialog ::v-deep .el-dialog__header {
    background: #409eff;
  }
  
  .searchDialog ::v-deep .el-dialog__title {
    color: #fff;
  }
  
  .searchDialog ::v-deep .el-dialog__headerbtn .el-dialog__close {
    color: #fff;
  }
  </style>
  